package com.woniu.woniuproject.controller.admin;

import com.github.pagehelper.PageInfo;
import com.woniu.woniuproject.service.UserVoService;
import com.woniu.woniuproject.utils.ResponseResult;
import com.woniu.woniuproject.vo.UserVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author EDZ
 * @create 2020/11/18
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserVoService userVoService;

    //查询所有的用户
    @GetMapping("/queryUserList")
    public ResponseResult<PageInfo<UserVo>> getUserList(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                                        @RequestParam(value = "pageSize", defaultValue = "3") int pageSize,
                                                        @RequestParam(value = "username", defaultValue = "") String username,
                                                        @RequestParam(value = "userphone", defaultValue = "") String userphone,
                                                        @RequestParam(value = "name", defaultValue = "") String name,
                                                        @RequestParam(value = "state", defaultValue = "-1")String state,
                                                        @RequestParam(value = "Grade", defaultValue = "-1")String Grade,
                                                        @RequestParam(value = "type", defaultValue = "-1")String type
                                                        ) {
        int st=Integer.parseInt(state);
        int gr=Integer.parseInt(Grade);
        int tp=Integer.parseInt(type);
        System.out.println(st+"---------------");
        PageInfo<UserVo> pageInfo = userVoService.queryUserVoList(currentPage, pageSize,username,userphone,name,
                st,gr,tp

                );
        return new ResponseResult<>(pageInfo);
    }

    //修改用户可以状态
    @GetMapping("/enableUserList")
    public String enableUserList(@Param("uid") String uid){
        String[] split = uid.split(",");
        int[] array = Arrays.stream(split).mapToInt(Integer::parseInt).toArray();
        for(int a:array) {
            System.out.println(a+"+++++++++++++");
        }
        int rows=userVoService.updateUser(array);
        System.out.println(rows+"---------------");
            if (rows>0){
                return "0";//启动账号成功
            }else {
                return "1";//用户已经启动账号
            }

        //System.out.println(rows+"---------------------");
    }
    @GetMapping("/disableUserList")
    public String disableUserList(@Param("uid") String uid){
        String[] split = uid.split(",");
        int[] array = Arrays.stream(split).mapToInt(Integer::parseInt).toArray();
        for(int a:array) {
            System.out.println(a+"+++++++++++++");
        }
        int rows=userVoService.updatedisableUser(array);
        System.out.println(rows+"---------------");
        if (rows>0){
            return "0";//禁用账号成功
        }else {
            return "1";//禁用账号失败
        }
    }
}
